package com.example.carrierapp.ui.login;

import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;

import com.example.carrierapp.base.MVPBaseActivity;
import com.example.carrierapp.config.ConstValue;
import com.example.carrierapp.util.ActivityUtil;
import com.example.carrierapp.util.SPUtils;
import com.example.carrierapp.util.ToastUtils;
import com.example.lb.fingerdemo.R;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class LoginActivity extends MVPBaseActivity<LoginContract.View, LoginPresenter> implements LoginContract.View {
    @BindView(R.id.et_login_number)
    EditText mEtNumber;
    @BindView(R.id.et_login_password)
    EditText mEtPassWord;
    @BindView(R.id.cb_login_remember)
    CheckBox mCbRemember;
    @BindView(R.id.iv_login_password_clear)
    ImageView mIbPassClear;
    @BindView(R.id.iv_login_number_clear)
    ImageView mIbNumberClear;


    private String mNumber;
    private String mPassWord;
    private boolean m2Main;


    @Override
    public int getContentView() {
        return R.layout.activity_login;
    }

    @Override
    public void initView() {
        super.initView();
        ButterKnife.bind(this);

        mCbRemember.setChecked(SPUtils.getBoolean(this, ConstValue.AUTO_LOGIN, false));
        //自动登录&&登录状态
        if (SPUtils.getBoolean(this, ConstValue.AUTO_LOGIN, false) && SPUtils.getBoolean(this, ConstValue.LOGIN_STATE, false)) {
            mEtPassWord.setText(SPUtils.getString(this, ConstValue.LOGIN_PASS, ""));
            mPassWord = SPUtils.getString(this, ConstValue.LOGIN_PASS, "");
            if (!TextUtils.isEmpty(mPassWord)) {
                mIbPassClear.setVisibility(View.VISIBLE);
            }
        }
        mEtNumber.setText(SPUtils.getString(this, ConstValue.LOGIN_COUNT, ""));
        mNumber = SPUtils.getString(this, ConstValue.LOGIN_COUNT, "");
        if (!TextUtils.isEmpty(mNumber)) {
            mIbNumberClear.setVisibility(View.VISIBLE);
        }

    }

    @Override
    public void initData() {
        super.initData();
        m2Main = getIntent().getBooleanExtra(ConstValue.LOGIN_2MAIN, true);
    }

    @Override
    public void initEvent() {
        super.initEvent();
        mEtNumber.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (!TextUtils.isEmpty(mEtNumber.getText().toString().trim())) {
                    mIbNumberClear.setVisibility(View.VISIBLE);
                } else {
                    mIbNumberClear.setVisibility(View.GONE);
                }
                mNumber = mEtNumber.getText().toString().trim();

            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        mEtPassWord.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (!TextUtils.isEmpty(mEtPassWord.getText().toString().trim())) {
                    mIbPassClear.setVisibility(View.VISIBLE);
                } else {
                    mIbPassClear.setVisibility(View.GONE);
                }
                mPassWord = mEtPassWord.getText().toString().trim();
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        mCbRemember.setOnCheckedChangeListener((buttonView, isChecked) ->
                {
                    SPUtils.putBoolean(LoginActivity.this, ConstValue.AUTO_LOGIN, isChecked);
                }
        );
    }

    @Override
    public String getTitleString() {
        return null;
    }

    @OnClick({R.id.tv_login_forget, R.id.tv_login_login, R.id.iv_login_password_clear, R.id.iv_login_number_clear})
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_login_forget:
                ActivityUtil.startForget(this, m2Main);
                break;
            case R.id.tv_login_login:
                if (TextUtils.isEmpty(mNumber)) {
                    ToastUtils.showCenterToast(this, "账号不能为空！");
                } else if (TextUtils.isEmpty(mPassWord)) {
                    ToastUtils.showCenterToast(this, "密码不能为空！");
                } else {
                    showLoading("登录中。。。");
                    mPresenter.login(mNumber, mPassWord);
                }
                break;
            case R.id.iv_login_password_clear:
                mEtPassWord.setText("");
                break;
            case R.id.iv_login_number_clear:
                mEtNumber.setText("");
                break;
        }
    }

    @Override
    public void loginSuccess(String msg) {
        dismissProgress();
        if (m2Main) {
            ActivityUtil.startMainActivity(this);
        }
        finish();
    }

    @Override
    public void loginFailde(String msg) {
        dismissProgress();
        ToastUtils.showCenterToast(this, msg);
    }

    @Override
    protected LoginPresenter createPresenter() {
        return new LoginPresenter();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        // 过滤按键动作，按返回键不退出程序，直接退到桌面
        if (event.getAction() == KeyEvent.ACTION_DOWN && keyCode == KeyEvent.KEYCODE_BACK) {
            moveTaskToBack(true);//true对任何Activity都适用
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
}
